
// Definition for a binary tree node.
function TreeNode(val, left, right) {
    this.val = (val===undefined ? 0 : val)
    this.left = (left===undefined ? null : left)
    this.right = (right===undefined ? null : right)
}

/**
 * @param {TreeNode} root
 * @param {number} k
 * @return {boolean}
 */
var findTarget = function(root, k) {
  let val = [];
  function preView(node) {
    if(!node) return;
    val.push(node.val);
    preView(node.left);
    preView(node.right);
  }
  preView(root);
  val.sort((a, b) => a - b);
  let i = 0, j = val.length - 1;
  while(i < j) {
    if(val[i] + val[j] == k) {
      return true;
    }
    else if(val[i] + val[j] > k) {
      j--;
    } else {
      i++;
    }
  }
  return false;
};
